<!-- 注册页面 -->
<template>
  <div>
      <div id="outDiv">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="账号" prop="UserName">
    <el-input v-model="ruleForm.UserName"></el-input>
  </el-form-item>
        <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="确认密码" prop="checkPass">
    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
  </el-form-item>
 
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
</div>

<Special/>
  </div>
</template>

<script>
import Special from '@/components/Special/special.vue';
export default {
    data() {
        var checkName = (rule, value, callback) => {//判断用户名
      if (!value) {
        return callback(new Error("账号不能为空"));
      }
      setTimeout(() => {
        if (String(value).length < 2) {
          callback(new Error("用户名字错误！！"));
        } else {
          callback();
        }
      }, 1000);
    };
        var validatePass = (rule, value, callback) => {//判断密码
            if (value === "") {
                callback(new Error("请输入密码"));
            }
            else {
                if (this.ruleForm.checkPass !== "") {
                    this.$refs.ruleForm.validateField("checkPass");
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {//判断再次输入密码
            if (value === "") {
                callback(new Error("请再次输入密码"));
            }
            else if (value !== this.ruleForm.pass) {
                callback(new Error("两次输入密码不一致!"));
            }
            else {
                callback();
            }
        };
        return {
            ruleForm: {
                pass:"",
                checkPass: "",
                UserName: ""
            },
            rules: {
                pass: [
                    { validator: validatePass, trigger: "blur" }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: "blur" }
                ],
                UserName: [
                    { validator: checkName, trigger: "blur" }
                ]
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$store.commit("ADD",this.ruleForm)
                    this.$router.push("/login")
                    this.$notify({
                        title: "成功",
                        message: `注册成功！！`,
                        type: "success",
                        duration:1000
                        });
                }
                else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    },
    components: { Special }
}
</script>

<style lang="less" scoped>
 #outDiv{
   float: left;
   margin: 30vh auto;
   z-index: -1;
  }

</style>